//引入axios
import axios from 'axios'
import { Message } from 'element-ui';
//创建一个新的axios实例
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_URL,
    //设置超时时间，超过该时间就不会发起请求
    timeout: 3000
})
service.interceptors.request.use(
    config => {
        config.headers = {
            'Content-Type': 'application/json' //配置请求头
        }
        config.headers["token"] = '' //配置tokenCookies.get('user_token')
        return config
    },
    error => {
        console.log(error);
        return Promise.reject(error)
    }
)
service.interceptors.response.use(
    //请求成功处理
    response => {
        if (response.data.code === 1) {
            return response.data
        } else {
            console.log(response);
        }
    },
    error => {
        if (JSON.stringify(error).includes('timeout')) {
            Message.$message.error('服务器响应超时，请刷新当前页');
        } else {
            Message.$message.error(error.msg);
        }
    }
)
export default service